package ai;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgBorder
{
	public static int [] displayPercent(float [] imgInfo)
	{
		int [] result = new int[imgInfo.length];
		for(int i = 0, j = 0; i < imgInfo.length; i++)
		{
			if((j <= 5))
			{
				if(imgInfo[i] > 0.11)
				{
					System.out.print(imgInfo[i] + ":" + i + "\t");
					result[i] = 1;
					j++;
				}
			}else
			{
				j = 0;
				System.out.println();
			}
		}
		System.out.println();
		return result;
	}
	
	public static void main(String [] args) throws IOException
	{
		BufferedImage bi = ImageIO.read(new File("D:/全屏截图-2_外框图.png"));//通过imageio将图像载入  
		int h = bi.getHeight();//获取图像的高  
		int w = bi.getWidth();//获取图像的宽  
		int max = new Color(255, 255, 255).getRGB();
		System.out.println("图像宽高为：" + w + "," + h);
		float imgInfo_h[] = new float[h];
		float imgInfo_w[] = new float[w];
		for(int k = 0; k < h; k++)
		{
			int whitePoint = 0;
			for(int l = 0; l < w; l++)
			{
				if(bi.getRGB(l, k) == max)
				{
					whitePoint++;
				}
			}
			imgInfo_h[k] = whitePoint / (float) h;
		}
		for(int k = 0; k < w; k++)
		{
			int whitePoint = 0;
			for(int l = 0; l < h; l++)
			{
				if(bi.getRGB(k, l) == max)
				{
					whitePoint++;
				}
			}
			imgInfo_w[k] = whitePoint / (float) w;
		}
		System.out.println("垂直上的可见线条位置：");
		int [] whiteLineWidth = ImgBorder.displayPercent(imgInfo_w);
		System.out.println("水平上的可见线条位置：");
		int [] whiteLineHeight = ImgBorder.displayPercent(imgInfo_h);
		BufferedImage nbi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		for(int i = 0; i < w; i++)
		{
			if(whiteLineWidth[i] == 1)
			{
				for(int j = 0; j < h; j++)
				{
					nbi.setRGB(i, j, max);
				}
			}
		}
		for(int i = 0; i < h; i++)
		{
			if(whiteLineHeight[i] == 1)
			{
				for(int j = 0; j < w; j++)
				{
					nbi.setRGB(j, i, max);
				}
			}
		}
		ImageIO.write(nbi, "png", new File("D:/全屏截图-2_外框还原图.png"));
	}
}
